<?php

namespace App\Services\Menu;

use App\Models\Menu;
use Illuminate\Support\Collection;


class MenuService
{
    public function init()
    {
        if (Menu::count() == 0) {
            $menus = config('menus');
            array_map(function ($menu) {
                Menu::create($menu);
            }, $menus);
        }

    }

    protected function deepPutMenus($data)
    {
        //递归获取所有节点
        foreach ($data as $item) {
            Menu::create(['name' => $item['name'], 'title' => $item['meta']['title'], 'description' => $item['meta']['description'], 'guard_name' => 'sanctum']);
            if (isset($item['children'])) {
                $this->getDeepPermission($item['children']);
            }
        }
    }

    public function filterMenus($permissions)
    {
        $menuIds = [];
        foreach ($permissions as $permission) {
            array_push($menuIds, $permission->menu_id);
        }
        $menus = Menu::whereIn('id', $menuIds)->get();
        return $menus->toTree();
    }
}
